package com.yanq.liancar.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import com.vondear.rxtool.view.RxToast;
import com.yanq.liancar.R;
import com.yanq.liancar.activity.base.BaseToolbarActivity;
import com.yanq.liancar.adapter.BigSelectClassifyAdapter;
import com.yanq.liancar.bean.SelectClassifyBean;
import com.yanq.liancar.url.Config;
import com.yanq.liancar.util.Utils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class SmallBClassificationActivity extends BaseToolbarActivity {
    private List<SelectClassifyBean.DataBean> mList=new ArrayList<>();
    @BindView(R.id.recyclerview)
    RecyclerView mRecyclerView;
    private BigSelectClassifyAdapter mBigSelectClassifyAdapter;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_smallb_classification);
        Utils.setStatusBar(this, true,false);
        ButterKnife.bind(this);
        mTvTitle.setText("商品分类");
        initOkgo();
        initRecyclerView();
    }

    private void initRecyclerView() {
        mRecyclerView.setNestedScrollingEnabled(false);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        mBigSelectClassifyAdapter=new BigSelectClassifyAdapter(this,mList);
        mRecyclerView.setAdapter(mBigSelectClassifyAdapter);
    }


    private void initOkgo() {
        OkGo.<String>post(Config.s + "llc/api/selectClassify")
                .tag(this)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Gson gson = new Gson();
                        SelectClassifyBean selectClassifyBean = gson.fromJson(response.body(), SelectClassifyBean.class);
                        if (selectClassifyBean.isIsSuccess()) {
                            mList.clear();
                            mList.addAll(selectClassifyBean.getData());
                            mBigSelectClassifyAdapter.setData(mList);
                        }else {
                            RxToast.error(selectClassifyBean.getOutputMessage());
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        RxToast.error("服务器连接错误");
                    }
                });
    }

}
